<template>
  <el-dialog
    :title="form.id ?'修改学科':'添加学科'"
    :visible.sync="dialogShow"
    width="40%"
    @close="onclose"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="学科名称" label-width="100px" prop="subject_name">
        <el-input v-model="form.subject_name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="排序" label-width="100px" prop="sort">
        <el-input-number v-model="form.sort" :min="1" :max="10" label="描述文字" @change="handleChange" />
      </el-form-item>
      <el-form-item prop="is_show" label="是否显示" label-width="100px">
        <el-switch
          v-model="form.is_show"
          active-text="是"
          inactive-text="否"
          :active-value="1"
          :inactive-value="2"
        />
      </el-form-item>
      <el-form-item label="学科图标" label-width="100px">
        <el-upload
          action="#"
          list-type="picture-card"
          :file-list="fileList"
          :on-preview="handlePictureCardPreview"
          :http-request="httpRequest"
        >
          <i class="el-icon-plus" />
          <div slot="tip" class="el-upload__tip">建议尺寸：200*200</div>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible" :modal-append-to-body="false">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogShow= false">取 消</el-button>
      <el-button type="primary" @click="uploadInfo">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updataImg, addXuekeInfo, updataInfo } from '@/api/subject'
export default {
  name: 'CtsAddsubject',
  // props: {
  //   dangeInfo: {
  //     type: Object,
  //     default: () => {}
  //   }
  // },
  data() {
    return {
      fileList: [],
      dialogShow: false,
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        subject_name: '',
        sort: '',
        is_show: '',
        subject_icon: ''
      },
      tuName: '',
      rules: {
        subject_name: [{ required: true, message: '必填', trigger: 'blur' }],
        sort: [{ required: true, message: '必填', trigger: 'blur' }],
        is_show: [{ required: true, message: '必填', trigger: 'blur' }]
      }
    }
  },

  methods: {
    // 取消
    onclose() {
      this.form = {
        subject_name: '',
        sort: '',
        is_show: '',
        subject_icon: ''
      }
      this.fileList = []
      this.$refs.form.resetFields()
      this.$refs.form.clearValidate()
    },
    // 更改
    async uploadInfo() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.form.id ? await updataInfo(this.form) : await addXuekeInfo(this.form)
      // if (data === null) { // 错了！！！！！！！！！！！！！！！！！
      //   return this.$message.error()
      // }
      // 提示效果
      this.$message.success(this.form.id ? '修改成功' : '添加成功')
      this.$emit('updateList')
      this.$refs.form.resetFields()
      this.dialogShow = false
    },
    // 上传图片
    async httpRequest(res) {
      console.log(res.file)
      const fd = new FormData()
      fd.append('file', res.file)
      // 上传文件接口
      const resu = await updataImg(fd)
      console.log(resu)
      this.subject_icon = resu.url
      this.tuName = resu.original_name
    },

    handleChange(value) {
      console.log(value)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      console.log(file)
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
